﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// Generic class for operator performence monitor
    /// </summary>
    /// <typeparam name="T">Generic operator type.</typeparam>
    public class OperatorPerformenceMonitor<T> where T : IIdentifiable
    {
        /// <summary>
        /// The dictionary
        /// </summary>
        protected Dictionary<Guid, OperatorPerformenceBinding<T>> dictionary = new Dictionary<Guid, OperatorPerformenceBinding<T>>();

        #region Properties

        /// <summary>
        /// Gets the <see cref="`0"/> with the specified key.
        /// </summary>
        /// <value>
        /// The <see cref="`0"/>.
        /// </value>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public T this[Guid key]
        {
            get
            {
                return this.GetOperatorByKey(key);
            }
        }

        /// <summary>
        /// Gets the opereator count.
        /// </summary>
        /// <value>
        /// The opereator count.
        /// </value>
        public int OpereatorCount
        {
            get
            {
                return this.dictionary.Keys.Count();
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="OperatorPerformenceMonitor{T}" /> class.
        /// </summary>
        /// <param name="operators">The operators.</param>
        public OperatorPerformenceMonitor(IEnumerable<T> operators = null)
        {
            if (operators != null)
            {
                foreach (T one in operators)
                {
                    if (!dictionary.ContainsKey(one.Key))
                    {
                        dictionary.Add(one.Key, new OperatorPerformenceBinding<T>(one));
                    }
                }
            }
        }

        #endregion

        /// <summary>
        /// Gets the operator by key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public T GetOperatorByKey(Guid key)
        {
            return this.dictionary.ContainsKey(key) ? this.dictionary[key].Operator : default(T);
        }

        /// <summary>
        /// Gets the most available operator.
        /// </summary>
        /// <returns></returns>
        public T GetMostAvailableOperator()
        {
            double rank = double.MinValue;
            OperatorPerformenceBinding<T> result = null;
            foreach (var key in this.dictionary.Keys)
            {
                if (this.dictionary[key].AvailabilityRank > rank)
                {
                    result = this.dictionary[key];
                    rank = result.AvailabilityRank;
                }
            }

            return result == null ? default(T) : result.Operator;
        }

        /// <summary>
        /// Adds the or upadate operator.
        /// </summary>
        /// <param name="operator">The operator.</param>
        public void AddOrUpadateOperator(T @operator)
        {
            if (@operator != null)
            {
                if (this.dictionary.ContainsKey(@operator.Key))
                {
                    this.dictionary[@operator.Key] = new OperatorPerformenceBinding<T>(@operator);
                }
                else
                {
                    this.dictionary.Add(@operator.Key, new OperatorPerformenceBinding<T>(@operator));
                }
            }
        }
    }
}
